﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using HRProject.Code.DAO;
using HRProject.Code.Common;
using System.Collections.Generic;
using HRProject.Code.DTO;

namespace HRProject.Department
{
    public partial class EditDepartment : System.Web.UI.Page
    {
        private Guid deptId = Guid.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string id = Request.Params["id"];
                try
                {
                    deptId = new Guid(id);
                }
                catch (Exception exp)
                {
                    Console.Out.Write(exp.StackTrace);
                    deptId = Guid.Empty;
                }
                DepartmentDAO dao = new DepartmentDAO();
                BLDepartment filter = new BLDepartment();
                filter.Id = deptId;
                filter.Name = string.Empty;
                filter.Parent = new BLDepartment();
                filter.Parent.Id = Guid.Empty;
                List<BLDepartment> departments = dao.search(filter);
                if (departments.Count > 0)
                {
                    hdfId.Value = deptId.ToString();
                    tbxName.Text = departments[0].Name;
                    tbxParent.Text = departments[0].Parent.Name;
                    txaDescription.Text = departments[0].Description;
                    imgIcon.ImageUrl = departments[0].IconUrl;
                }
            }
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            if (!BLAuthentication.IsEditAllowed)
            {
                lblError.Text = ContentProvider.GetMessage("NotAuthorized");
                return;
            }

            BLDepartment department = new BLDepartment();
            try
            {
                deptId = new Guid(hdfId.Value);
            }
            catch (Exception exp)
            {
                Console.Out.Write(exp.StackTrace);
                deptId = Guid.Empty;
            }
            department.Id = deptId;
            department.Name = tbxName.Text;
            department.Description = txaDescription.Text;
            department.DataSignature = 0;

            string iconUrl = string.Empty;
            if (cbxIcon.Checked && fulIcon.HasFile)
            {
                try
                {
                    string ext = fulIcon.PostedFile.FileName.Substring(fulIcon.PostedFile.FileName.LastIndexOf('.'));
                    iconUrl = "/image/department/" + department.Name + ext;
                    fulIcon.PostedFile.SaveAs(Server.MapPath(iconUrl));
                    department.IconUrl = iconUrl;
                }
                catch (Exception ex)
                {
                    lblError.Text = "ERROR: " + ex.Message.ToString();
                }
            }
            else
            {
                lblError.Text = "You have not specified a file.";
            }

            DepartmentDAO dao = new DepartmentDAO();
            dao.update(department);

            Util.CloseFrame(Response);
        }
    }
}
